package com.aaa.operations.controller;


import com.aaa.common.entity.Repair;
import com.aaa.common.entity.ResultData;
import com.aaa.operations.service.RepairService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/repair")
public class RepairController {
    /**
     * 查询所有寝室
     * @return
     */
    @Autowired
    private RepairService repairService;
    @ResponseBody
    @RequestMapping("/getAllRepair")
    public ResultData getAllRoom(Integer pageSize, Integer pageNum, String roomNo){
        Map map = new HashMap();
        if(roomNo!=null && !roomNo.equals("")){
            map.put("roomNo",roomNo);
        }

        PageInfo pageInfo = repairService.getAllRepair(pageSize,pageNum,map);
        System.out.println(pageInfo);
        if(pageInfo!=null){
            return new ResultData(true,"查询成功",pageInfo);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }
    @RequestMapping("/addRepair")
    public ResultData addRepair(@RequestBody Repair repair){
        repair.setRepairState(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss");
        String currentDate =   dateFormat.format( new Date() );
        repair.setRepairSolvetime(currentDate);

        boolean insert = repairService.insert(repair);
        if (insert) {
            return new ResultData(true, "上报成功", null);
        } else {
            return new ResultData(false, "上报失败", null);
        }
    }

    //维修完成
    @RequestMapping("/slove")
    public ResultData slove(Integer repairId){
        Repair repair = new Repair();
        repair.setRepairId(repairId);
        repair.setRepairState(1);
        SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss");
        String currentDate =   dateFormat.format( new Date() );
        repair.setRepairTime(currentDate);

        boolean b = repairService.updateById(repair);
        if (b) {
            return new ResultData(true, "维修完毕", null);
        } else {
            return new ResultData(false, "维修失败", null);
        }
    }
}

